const app = getApp()
const request = require('../../utils/request.js')


Page({
  data: {
    stepLog: [],
    hasData: false
  },

  onShow: function() {
    wx.setNavigationBarTitle({
      title: '步数明细',
    });

    request.getStepLog().then(res => {
      if (res.code == 0 && res.type == 1) {
        let data = this.reversedSort(res.details);
        for (let i = 0; i < data.length; i++) {
          data[i].ts = this.stamp2time(data[i].ts);
          data[i].type = this.type2str(data[i].type);
        }
        
        this.setData({
          stepLog: data,
          hasData: data.length>0? true: false
        });
      }
    })
  },

  // sort
  reversedSort: function(data){
    return data.sort(function (a, b) {
      if (a.ts > b.ts) {
        return -1;
      } else {
        return 1;
      }
    });
  },

  // 时间戳转时间
  stamp2time: function(stamp) {

    //如果date为13位不需要乘1000
    let date = new Date(stamp * 1000);
    let Y = date.getFullYear() + '/';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
    let D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
    // let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
    // let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
    // let s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
    return Y + M + D;
  },

  // 判断类型
  type2str: function(types) {
    switch (types) {
      case "1":
        return "邀请好友";
        break;
      case "2":
        return "微信步数";
        break;
      case "3":
        return "看广告";
        break;
      case "4":
        return "赚步数";
        break;
      case "5":
        return "兑换";
        break;     
      case "6":
        return "好友获得";
        break;
      case "7":
        return "看广告";
        break;    
      default:
        return "赚步数";
    }
  }


})